home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / parallel / mulinda < prev    next >
Text File  |  1992-04-11  |  7KB  |  131 lines

  1. /*===================================================================*/
  2. /*----muProlog specifics, using a communicator                       */
  3. /*===================================================================*/
  4. /*---Start up the communicator, with pipe connections                */
  5. set_up_server_communications__(Server_machine,Client_machines,
  6. Communications_information):-
  7.     pipe(Read1,Write1),
  8.     pipe(Read2,Write2),
  9.     start_communicator__(Read1,Write1,Read2,Write2,Client_machines).
  10. /*-------------------------------------------------------------------*/
  11. /*----Start the communicator process, passing the appropriate ends of*/
  12. /*----the pipe in the exec                                           */
  13. start_communicator__(Channel_from_communicator,Write1,Read2,
  14. Channel_to_communicator,Client_machines):-
  15.     fork,
  16. /*----This is the server process here                                */
  17.     close(Write1),
  18.     close(Read2),
  19.     asserta(channel_from_communicator__(Channel_from_communicator)),
  20.     asserta(channel_to_communicator__(Channel_to_communicator)),
  21.     Start_machines_term =.. [machines|Client_machines],
  22.     write_term_to_communicator__(Start_machines_term).
  23.  
  24. start_communicator__(Read1,Channel_to_server,Channel_from_server,
  25. Write2,_):-
  26. /*----This is the communicator process                               */
  27.     close(Read1),
  28.     close(Write2),
  29.     get_descriptor_atom__(Channel_to_server,Write_atom),
  30.     get_descriptor_atom__(Channel_from_server,Read_atom),
  31.     execlp(communicator,communicator,Write_atom,Read_atom,z,z).
  32. /*-------------------------------------------------------------------*/
  33. /*----Stop the communicator by sending a message to it               */
  34. close_down_server_communications__(_,_,_):-
  35.     write_term_to_communicator__(exit),
  36.     retract(channel_from_communicator__(Channel_from_communicator)),
  37.     close(Channel_from_communicator),
  38.     retract(channel_to_communicator__(Channel_to_communicator)),
  39.     close(Channel_to_communicator).
  40. /*-------------------------------------------------------------------*/
  41. /*----Clients to not need any communications setup                   */
  42. /*----set_up_client_communications__(_,_,_).
  43. /*-------------------------------------------------------------------*/
  44. /*----Clients do not need any communications shutdown                */
  45. close_down_client_communications__(_,_,_).
  46. /*-------------------------------------------------------------------*/
  47. /*----Nothing to do on power up for other machines                   */
  48. power_up_machine__(Machine).
  49. /*-------------------------------------------------------------------*/
  50. /*----Nothing to do on power down for a machine                      */
  51. power_down_machine__(Machine).
  52. /*-------------------------------------------------------------------*/
  53. /*----To start a client, send a message to the communicator. It will */
  54. /*----do an rexec to get the machine going, and waiting for a        */
  55. /*----client_eval__.                                                 */
  56. start_client__(Machine):-
  57.     write_term_to_communicator__(start(Machine)).
  58. /*-------------------------------------------------------------------*/
  59. /*----To stop a machine, its pipe must be closed by the communicator */
  60. stop_client__(Machine):-
  61.     write_term_to_communicator__(stop(Machine)).
  62. /*-------------------------------------------------------------------*/
  63. /*----This is to send a message to a client. Simply pass to          */
  64. /*----communicator                                                   */
  65. send_term_to_client__(Machine,Term):-
  66.     debug_network__(forward(Machine,Term)),
  67.     write_term_to_communicator__(forward(Machine,Term)).
  68. /*-------------------------------------------------------------------*/
  69. /*----For the server to receive a term, read from the communicator   */
  70. receive_term_from_any_client__(Term):-
  71.     debug_network__(waiting_for_term_from_client),
  72.     read_term_from_communicator__(Term),
  73.     debug_network__(from_communicator(Term)).
  74. /*-------------------------------------------------------------------*/
  75. /*----This gets a term from the server, when in a client. As the io  */
  76. /*----is vis stdin and stdout, simply read a term.                   */
  77. receive_term_from_server__(Term):-
  78.     debug_network__(waiting_for_term_from_server),
  79.     read(A_term),
  80.     debug_network__(from_server(A_term)),
  81.     A_term = Term.
  82. /*-------------------------------------------------------------------*/
  83. /*----For a client to send a term to the server, simply write to     */
  84. /*----stdout. See above for comment.                                 */
  85. send_term_to_server__(Term):-
  86.     debug_network__(to_server(Term)),
  87.     write_term__(forward(server(1),Term)).
  88. /*-------------------------------------------------------------------*/
  89. /*----Write a term to the communicator, down the channel             */
  90. write_term_to_communicator__(Term):-
  91.     channel_to_communicator__(Channel_to_communicator),
  92.     write_term_to_channel__(Channel_to_communicator,Term).
  93. /*-------------------------------------------------------------------*/
  94. /*----Read a term from the communicator channel                      */
  95. read_term_from_communicator__(Term):-
  96.     channel_from_communicator__(Channel_from_communicator),
  97. /*----Because some Prologs won't allow non-variables to be read, read*/
  98. /*----into a new variable and instantiate afterwards                 */
  99.     read(Channel_from_communicator,A_term),
  100.     A_term = Term.
  101. /*-------------------------------------------------------------------*/
  102. /*----Writing a term must be followed by writing a .                 */
  103. write_term__(Term):-
  104.     write(Term),
  105.     writeln('.').
  106. /*-------------------------------------------------------------------*/
  107. /*----Writing a term must be followed by writing a .                 */
  108. write_term_to_channel__(Channel,Term):-
  109.     write(Channel,Term),
  110.     writeln(Channel,'.').
  111. /*-------------------------------------------------------------------*/
  112. /*----Get an atom of a file descriptor for a channel                 */
  113. get_descriptor_atom__(Channel,Atom):-
  114.     descriptor(Channel,Descriptor),
  115.     integer_atom__(Descriptor,Atom).
  116. /*-------------------------------------------------------------------*/
  117. /*----Convert a two digit integer to an atom                         */
  118. integer_atom__(Integer,Atom):-
  119.     Tens is (Integer / 10) + 48,
  120.     Ones is (Integer mod 10) + 48,
  121.     name(Atom,[Tens,Ones]).
  122. /*-------------------------------------------------------------------*/
  123. debug_network__(Term):-
  124.     debugging_net__,
  125.     !,
  126.     write(Term),
  127.     nl.
  128.  
  129. debug_network__(_).
  130. /*-------------------------------------------------------------------*/
  131.